/* HOME */
define(function() {
    return {
        go: function() {
            $loading();
            this.offset = 0;
            this.amount = 50;
            this.page = 0;
            this.showPhotos();
        },
        showPhotos: function(feed) {
            /* If there is no cached feed, we should probably fetch it... or tell the backend to fetch it */
            if(!feed) {
                var self = this;
                var limit = this.offset + this.amount;
                this.offset += limit;
                FB.api("me/home", {limit: limit, filter: 'app_2305272732'}, function(res) {
                    $('#footer').hide();
                    self.showPhotos(res);
                });
                return;
            }
            
            feed.data = postParser(feed.data);
            var self = this;
            /* Template the data and show it */
            Template('photos', {posts: feed.data}, function(html) {
                require(['masonry'], function() {
                    if(self.page == 0) {
                        $content.html(html).imagesLoaded( function(){
                            $content.find('.photos').masonry({
                                itemSelector : '.sheet'
                            });
                        });
                    } else {
                        var $boxes = $(html).find('.sheet_a');
                        $content.find('.photos').append($boxes).imagesLoaded( function(){
                            $content.find('.photos').masonry('appended', $boxes);
                        });
                    }
                    self.page++;
                });
            }, ['likes','comments']);
        },
        infinite: function(e) {
            console.log(e);
        }
    };
});